<?php
/**
 * Created by PhpStorm.
 * User: HS032
 * Date: 2020/8/22
 * Time: 14:22
 */

namespace ticket\modules\backend\models;


class TicketOrderSearch extends OrderInfo
{

    public function rules()
    {
        return [
            ['goods_name','string'],
			['pay_number','string'],
            ['order_number','string'],
            ['order_status','integer'],
            ['create_time','safe'],
            ['is_finish','safe'],
        ];
    }


    public function search($params)
    {
        if (isset($params['goods_name']) and !empty($params['goods_name'])){
            $params['TicketOrderSearch']['goods_name'] = $params['goods_name'];
        }else{
            $params['TicketOrderSearch']['goods_name'] = '';
        }
		if (isset($params['pay_number']) and !empty($params['pay_number'])){
		    $params['TicketOrderSearch']['pay_number'] = $params['pay_number'];
		}else{
		    $params['TicketOrderSearch']['pay_number'] = '';
		}
        if (isset($params['order_number']) and !empty($params['order_number'])){
            $params['TicketOrderSearch']['order_number'] = $params['order_number'];
        }else{
            $params['TicketOrderSearch']['order_number'] = '';
        }
        // 订单状态筛选
        if (isset($params['order_status']) and !empty($params['order_status'])){
            $params['TicketOrderSearch']['order_status'] = $params['order_status'];
        }else{
            $params['TicketOrderSearch']['order_status'] = '';
        }
        // 手机号筛选
        if (isset($params['phone']) and !empty($params['phone'])){
            $phone = $params['phone'];
        }else{
            $phone = '';
        }
        // 姓名筛选
        if (isset($params['name']) and !empty($params['name'])){
            $name = $params['name'];
        }else{
            $name = '';
        }
        // 支付方式筛选
        if (isset($params['pay_type']) and !empty($params['pay_type'])){
            $pay_type = $params['pay_type'];
        }else{
            $pay_type = '';
        }
        // 是否使用优惠券
        if (isset($params['is_coupon']) and !empty($params['is_coupon'])){
            $is_coupon = $params['is_coupon'];
        }else{
            $is_coupon = '';
        }

        // 支付时间筛选
        if (!empty($params['begin']) and  !empty($params['end'])){
            $params['begin'] = $params['begin'].' 00:00:00';
            $params['end'] = $params['end'].' 23:59:59';
        }

        // 游玩时间
        if (!empty($params['start_reserve_time']) and  !empty($params['end_reserve_time'])){
            $start_reserve_time = $params['start_reserve_time'].' 00:00:00';
            $end_reserve_time = $params['end_reserve_time'].' 23:59:59';
        }


        $query = OrderInfo::find()->with(['phone'])->with(['ticket']);

        if ($this->load($params) and $this->validate()){
            if (!empty($params['begin']) and  !empty($params['end'])){
                $query->andFilterWhere(['between','tk_order_info.create_time',$params['begin'],$params['end']]);
            }
			$query->andFilterWhere(['like','pay_number',$this->pay_number]);
            if (!empty($params['TicketOrderSearch']['goods_name'])){
                $query->andFilterWhere(['like','goods_name',$this->goods_name]);
            }
            if (!empty($params['TicketOrderSearch']['order_number'])){
                $query->andFilterWhere(['order_number' => $this->order_number]);
            }
            if (!empty($params['TicketOrderSearch']['order_status'])){
                if ($params['TicketOrderSearch']['order_status'] == 5){
                    $query->andFilterWhere(['order_status' => 3,'is_finish' => 2]);
                }elseif($params['TicketOrderSearch']['order_status'] == 3){
                    $query->andFilterWhere(['order_status' => $this->order_status,'is_finish' => 1]);
                }else{
                    $query->andFilterWhere(['order_status' => $this->order_status]);
                }
            }
            if  ($is_coupon == 1){
                $query->andFilterWhere(['like','pay_note','门票']);
            }elseif ($is_coupon == 2){
                $query->andWhere(['or',['=','pay_note','{"coupon_name":"年卡折扣","coupons_type":"1","discount":"100"}'],['=','pay_note','']]);
            }
            if (!empty($pay_type)){
                $query->andFilterWhere(['like','pay_type',$pay_type]);

            }
            if (!empty($phone)){
                $query->joinWith(['phone'=>function ($q) use($phone){
                    $q->where(['like' ,'phone',$phone]);
                }]);
            }
            if (!empty($name)){
                $query->joinWith(['phone'=>function ($q) use($name){
                    $q->where(['like' ,'name',$name]);
                }]);
            }

            if (!empty($start_reserve_time) and !empty($end_reserve_time)){
                $query->joinWith(['phone'=>function ($q) use($start_reserve_time,$end_reserve_time){
                    $q->where(['between','reserve_time',$start_reserve_time,$end_reserve_time]);
                }]);
            }

        }

        return $query;
    }


}